package com.hua.yuan.ui;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.hua.yuan.base.Constents;
import com.hua.yuan.common.BaseActivity;
import com.hua.yuan.R;
import com.hua.yuan.ui.data.PlayDataRequest;
import com.hua.yuan.util.ToastUtil;
import com.hua.yuan.util.animation.AnimationListener;
import com.hua.yuan.util.animation.ViewAnimator;
import com.zhy.http.okhttp.callback.StringCallback;
import org.json.JSONException;
import org.json.JSONObject;
import java.net.URLDecoder;
import cm.hua.yuan.common.utils.PreferenceUtils;
import okhttp3.Call;

/**
 * Created by huangyang on 2018/5/27.
 * 充值界面
 */

public class CZActivity extends BaseActivity implements View.OnClickListener {


    private ImageView backImage;
    private TextView titleView;
    private TextView czMoney;//余额
    private TextView showMy;//充值的钱
    private TextView czBt;//充值按钮

    private ImageView mImage1;
    private ImageView mImage2;
    private ImageView mImage3;
    private ImageView mImage4;
    private ImageView mImage5;
    private ImageView mImage6;


    private ImageView aliPay;
    private ImageView weChatPay;
    private RelativeLayout selectPay;
    private LinearLayout zhifuLl;

    private String payM = "6";

    private boolean payFlag = true;

    private String allMoney;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_cz);

        allMoney = getIntent().getStringExtra("money");

        initView();
        initData();

    }

    private void initView() {

        backImage = findViewById(R.id.backImage);
        titleView = findViewById(R.id.titleView);
        czMoney = findViewById(R.id.czMoney);
        showMy = findViewById(R.id.showMy);
        czBt = findViewById(R.id.czBt);

        mImage1 = findViewById(R.id.mImage1);
        mImage2 = findViewById(R.id.mImage2);
        mImage3 = findViewById(R.id.mImage3);
        mImage4 = findViewById(R.id.mImage4);
        mImage5 = findViewById(R.id.mImage5);
        mImage6 = findViewById(R.id.mImage6);

        aliPay = findViewById(R.id.aliPay);
        weChatPay = findViewById(R.id.weChatPay);
        selectPay = findViewById(R.id.selectPay);
        zhifuLl = findViewById(R.id.zhifuLl);


    }

    private void initData() {

        backImage.setOnClickListener(this);
        titleView.setText("代币充值");
        mImage1.setOnClickListener(this);
        mImage2.setOnClickListener(this);
        mImage3.setOnClickListener(this);
        mImage4.setOnClickListener(this);
        mImage5.setOnClickListener(this);
        mImage6.setOnClickListener(this);
        czMoney.setText(allMoney);
        czBt.setOnClickListener(this);


        selectPay.setOnClickListener(this);
        zhifuLl.setOnClickListener(this);
        weChatPay.setOnClickListener(this);
        aliPay.setOnClickListener(this);


    }

    @SuppressLint("SetTextI18n")
    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.backImage:
                finish();
                break;
            case R.id.mImage1:
                showMy.setText("6.00");
                payM = "6";

                mImage1.setImageResource(R.mipmap.money_6);
                mImage2.setImageResource(R.mipmap.money_12_1);
                mImage3.setImageResource(R.mipmap.money_18_1);
                mImage4.setImageResource(R.mipmap.money_25_1);
                mImage5.setImageResource(R.mipmap.money_50_1);
                mImage6.setImageResource(R.mipmap.money_98_1);


                break;
            case R.id.mImage2:
                showMy.setText("12.00");
                payM = "12";

                mImage1.setImageResource(R.mipmap.money_6_1);
                mImage2.setImageResource(R.mipmap.money_12);
                mImage3.setImageResource(R.mipmap.money_18_1);
                mImage4.setImageResource(R.mipmap.money_25_1);
                mImage5.setImageResource(R.mipmap.money_50_1);
                mImage6.setImageResource(R.mipmap.money_98_1);

                break;
            case R.id.mImage3:
                showMy.setText("18.00");
                payM = "18";

                mImage1.setImageResource(R.mipmap.money_6_1);
                mImage2.setImageResource(R.mipmap.money_12_1);
                mImage3.setImageResource(R.mipmap.money_18);
                mImage4.setImageResource(R.mipmap.money_25_1);
                mImage5.setImageResource(R.mipmap.money_50_1);
                mImage6.setImageResource(R.mipmap.money_98_1);

                break;
            case R.id.mImage4:
                showMy.setText("25.00");
                payM = "25";

                mImage1.setImageResource(R.mipmap.money_6_1);
                mImage2.setImageResource(R.mipmap.money_12_1);
                mImage3.setImageResource(R.mipmap.money_18_1);
                mImage4.setImageResource(R.mipmap.money_25);
                mImage5.setImageResource(R.mipmap.money_50_1);
                mImage6.setImageResource(R.mipmap.money_98_1);

                break;
            case R.id.mImage5:
                showMy.setText("50.00");
                payM = "50";

                mImage1.setImageResource(R.mipmap.money_6_1);
                mImage2.setImageResource(R.mipmap.money_12_1);
                mImage3.setImageResource(R.mipmap.money_18_1);
                mImage4.setImageResource(R.mipmap.money_25_1);
                mImage5.setImageResource(R.mipmap.money_50);
                mImage6.setImageResource(R.mipmap.money_98_1);

                break;
            case R.id.mImage6:
                showMy.setText("98.00");
                payM = "98";

                mImage1.setImageResource(R.mipmap.money_6_1);
                mImage2.setImageResource(R.mipmap.money_12_1);
                mImage3.setImageResource(R.mipmap.money_18_1);
                mImage4.setImageResource(R.mipmap.money_25_1);
                mImage5.setImageResource(R.mipmap.money_50_1);
                mImage6.setImageResource(R.mipmap.money_98);

                break;
            case R.id.czBt:

                showUi(true);

                break;

            case R.id.selectPay:
                showUi(false);
                break;
            case R.id.zhifuLl:
                break;
            case R.id.aliPay:
                //支付宝支付
                payFun("1");
                showUi(false);
                break;
            case R.id.weChatPay:
                //微信支付
                payFun("2");
                showUi(false);
                break;

        }
    }


    /**
     * 是否显示支付界面
     *
     * @param flag true表示显示 false表示不显示
     */
    private void showUi(boolean flag) {

        if (flag) {
            //显示支付选择
            selectPay.setVisibility(View.VISIBLE);
            ViewAnimator.animate(zhifuLl)
                    .translationY(400, 0)
                    .duration(300)
                    .onStart(new AnimationListener.Start() {
                        @Override
                        public void onStart() {
                            zhifuLl.setVisibility(View.VISIBLE);
                        }
                    }).start();

        } else {
            //隐藏支付选择
            ViewAnimator.animate(zhifuLl)
                    .translationY(0, 400)
                    .duration(300)
                    .onStop(new AnimationListener.Stop() {
                        @Override
                        public void onStop() {
                            zhifuLl.setVisibility(View.GONE);
                            selectPay.setVisibility(View.GONE);

                        }
                    }).start();


        }


    }


    /**
     * 支付功能
     *
     * @param type 1支付宝 2微信
     */
    private void payFun(String type) {

        if (!payFlag) {
            return;
        }
        payFlag = false;

        String payType;
        if ("1".equals(type)) {
            payType = "alipay";
        } else {
            payType = "wxpay";
        }

        String token = PreferenceUtils.getString(Constents.TOKEN);

        PlayDataRequest.payMoney(token, payType, "购买电影", payM, "2", "", payM, new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {

                Log.i("Test", "e = " + e.getMessage());

                ToastUtil.showShortToast("生成订单失败");
                payFlag = true;
            }

            @Override
            public void onResponse(String response, int id) {

                try {
                    JSONObject jsonObject = new JSONObject(response);

                    if (!jsonObject.isNull("errno")) {
                        String code = jsonObject.getString("errno");

                        if ("0".equals(code)) {

                            if (!jsonObject.isNull("url")) {
                                String url = jsonObject.getString("url");

                                url = toURLDecoded(url);

                                if (!TextUtils.isEmpty(url)) {

                                    Log.i("Test", "url = " + url);

                                    startActivity(new Intent(CZActivity.this, PlayActivity.class).putExtra("url", url));
                                    finish();
                                } else {
                                    ToastUtil.showShortToast("生成订单失败");
                                }

                            } else {
                                ToastUtil.showShortToast( "生成订单失败");
                            }

                        } else {
                            ToastUtil.showShortToast("生成订单失败");
                        }
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                    ToastUtil.showShortToast("生成订单失败");

                }

                payFlag = true;

            }
        });


    }


    private String toURLDecoded(String paramString) {
        if (paramString == null || paramString.equals("")) {
            return "";
        }

        try {
            String str = new String(paramString.getBytes(), "UTF-8");
            str = URLDecoder.decode(str, "UTF-8");
            return str;
        } catch (Exception localException) {

        }

        return "";
    }

}
